/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-11-11 20:17:00
 * Last Modified: 2016-01-21 17:26:09
 * File Name: controllers/password.go
 * Description:
 */
package controllers

import (
	"encoding/json"

	"passport/errorcode"
	"passport/password"
)

type PasswordNoLoginController struct {
	URLSignController
}

/*
	用户密码重置
*/
func (p *PasswordNoLoginController) MobilePasswdReset() {
	defer p.Output("MobilePasswdReset")

	body := p.Ctx.Input.RequestBody
	reqBody := &password.ResetPasswordReq{}
	if err := json.Unmarshal(body, reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	code, msg := password.GetPasswordService().ResetPassword(reqBody)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}

type PasswordController struct {
	LoginedController
}

/*
	用户密码重置
*/
func (p *PasswordController) ChangePassword() {
	defer p.Output("ChangePassword")

	body := p.Ctx.Input.RequestBody
	reqBody := &password.ChangePasswordReq{}
	if err := json.Unmarshal(body, reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	code, msg := password.GetPasswordService().ChangePassword(p.UserId, reqBody)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}
